
using System.Web.UI.HtmlControls;
using System;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;
using System.Collections;
using System.Web.UI;
using System.Web;


using com.ddsc.common;
//using com.ddsc.database;
using DDSC.OracleADO;
using com.ddsc.utility;
using log4net;
using log4net.Config;
using System.Data.OracleClient;


namespace smasb2b
{
	public class DOWN_AP_FORM : BasePage
	{
		
#region  Web Form 設計工具產生的程式碼
		
		//此為 Web Form 設計工具所需的呼叫。
		[System.Diagnostics.DebuggerStepThrough()]private void InitializeComponent()
		{
			
		}
		
		//注意: 下列預留位置宣告是 Web Form 設計工具需要的項目。
		//請勿刪除或移動它。
		private System.Object designerPlaceholderDeclaration;
		
		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: 此為 Web Form 設計工具所需的方法呼叫
			//請勿使用程式碼編輯器進行修改。
			InitializeComponent();
		}
		
#endregion
		
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			try
			{
				if (this.Request["VENDOR_TYPE"] != null)
				{
					string VENDOR_TYPE = System.Convert.ToString(this.Request["VENDOR_TYPE"].ToUpper().Trim());
					string FORM_TYPE = "";
					if (VENDOR_TYPE == "D")
					{
						FORM_TYPE = "CH";
					}
					else if (VENDOR_TYPE == "F")
					{
						FORM_TYPE = "EN";
					}
					if (FORM_TYPE != "")
					{
						//dbConnection dba = new dbConnection();
						//OracleConnection myConnection = dba.getOracleClientConnection();
						//string strSQL = string.Format("select FILE_NAME,FILE_CONTENT from VND_APFORM where FORM_TYPE=\'{0}\'  ", FORM_TYPE);
						//OracleCommand myCommand = new OracleCommand(strSQL, myConnection);
						//OracleDataAdapter MyDA = new OracleDataAdapter(myCommand);
						//DataSet ds = new DataSet();
						//MyDA.Fill(ds);
						//myConnection.Close();
						//myConnection.Dispose();

                        //Tony:參考VNDW0032將上段改寫成下段
                        dbAccess dba = new dbAccess();
                        ArrayList m_arrParam = new ArrayList();
                        string strSQL = "select FILE_NAME,FILE_CONTENT from VND_APFORM where FORM_TYPE=:FORM_TYPE  ";
                        m_arrParam.Add(dba.GetOracleParam(":FORM_TYPE", FORM_TYPE));
                        DataSet ds = dba.runExecuteQuery("DATA", strSQL, m_arrParam);

						byte[] byteBLOBData = null;
						string FILE_NAME = "";
						//-=============================-
						byteBLOBData = (byte[])ds.Tables["DATA"].Rows[0]["FILE_CONTENT"];
						FILE_NAME = System.Convert.ToString(ds.Tables["DATA"].Rows[0]["FILE_NAME"]);
						//StreamWriter m_StreamWriter = new StreamWriter(m_szTempFilePathHTML, false, System.Text.UnicodeEncoding.Unicode);
						string attFile = FILE_NAME.Substring(FILE_NAME.LastIndexOf("."));
						switch (attFile.ToUpper())
						{
							case ".DOC":
								Response.ContentType = "application/msword";
								break;
							case ".PDF":
								Response.ContentType = "application/pdf";
								break;
							case ".GIF":
							case ".JPG":
							case ".BMP":
								Response.ContentType = "image/html";
								break;
							default:
								Response.ContentType = "text/html";
								break;
						}
                        Response.ContentEncoding = System.Text.Encoding.UTF8;
						Response.AddHeader("content-disposition", "attachment;filename=" + FILE_NAME);
						Response.Clear();
						Response.BinaryWrite(byteBLOBData);
					}
				}
			}
			catch (Exception ex)
			{
				Response.Write(ex.Message);
			}
		}
		
	}
	
}
